package org.javaforever.gatescore.reports;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.widgets.Widget;

public class DiagramDataControlGrid extends Widget{
	protected FrontDomain domain;
	protected String detailPrefix;
	protected Set<FrontDomain> tranlateDomains = new TreeSet<>();
	protected Set<FrontDomain> parentTranlateDomains = new TreeSet<>();
	
	public DiagramDataControlGrid(String detailPrefix) {
		super();
		this.detailPrefix = detailPrefix;
	}
	
	@Override
	public StatementList generateWidgetStatements() {
		List<Writeable> sList = new ArrayList<>();
		long serial = 0L;
		
		sList.add(new Statement(serial+19000L,0,"<el-table :data=\"list\" :header-cell-style=\"{'background-color': '#3b8fce','color':'white'}\" :row-style=\"selectedHighlight\" v-loading=\"listLoading\" ref=\"table\" border fit highlight-current-row style=\"width: 100%\" @select-all=\"handleCheckedAll\" @selection-change=\"handleChecked\">"));
		sList.add(new Statement(serial+22000L,1,"<el-table-column type=\"selection\" align=\"center\" width=\"40px\"/>"));
		FrontField fid = this.domain.getDomainId()==null?null: this.domain.getDomainId();
		if (fid != null ) {
			sList.add(new Statement(serial+25000L,1,"<el-table-column width=\"80px\" align=\"center\" label=\""+fid.getText()+"\">"));
			sList.add(new Statement(serial+26000L,2,"<template slot-scope=\"scope\">"));
			sList.add(new Statement(serial+27000L,3,"<span>{{ scope.row."+fid.getFieldName()+" }}</span>"));
			sList.add(new Statement(serial+28000L,2,"</template>"));
			sList.add(new Statement(serial+29000L,1,"</el-table-column>"));
		}
		serial = serial+31000L;
		for (FrontField f:this.domain.getAllFieldsWithoutId()) {
			if (!(f instanceof FrontDropdown)) {
			if (f.getFieldType()!=null&&f.getFieldType().getTypeName()!=null&&f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
					sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\" :show-overflow-tooltip=\"true\">"));
					sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,3,"<img v-if=\"!scope.row."+f.getLowerFirstFieldName()+"\"　:src=\"'../static/images/blank.jpg'\" height=\"50\"/>"));
					sList.add(new Statement(serial+3000L,3,"<img v-else :src=\"'data:image/png;base64,'+scope.row."+f.getLowerFirstFieldName()+"\" height=\"50\"/>"));
					sList.add(new Statement(serial+4000L,2,"</template>"));
					sList.add(new Statement(serial+5000L,1,"</el-table-column>"));
				}else {
					sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,3,"<span>{{ scope.row."+f.getFieldName()+" }}</span>"));
					sList.add(new Statement(serial+3000L,2,"</template>"));
					sList.add(new Statement(serial+4000L,1,"</el-table-column>"));
				}
				serial = serial + 6000L;
			}else {
				FrontDropdown dp = (FrontDropdown)f;
				sList.add(new Statement(serial,1,"<el-table-column width=\"160px\" align=\"center\" label=\""+dp.getText()+"\">"));
				sList.add(new Statement(serial+1000L,2,"<template slot-scope=\"scope\">"));
				sList.add(new Statement(serial+2000L,3,"{{ scope.row."+dp.getAliasName()+" | "+dp.getTarget().getLowerFirstDomainName()+"Filter }}"));
				sList.add(new Statement(serial+3000L,2,"</template>"));
				sList.add(new Statement(serial+4000L,1,"</el-table-column>"));
				serial = serial + 6000L;
			}			
		}
		sList.add(new Statement(serial,0,"</el-table>"));
		sList.add(new Statement(serial+2000L,0,"<pagination v-show=\"total>0\" :total=\"total\" :page.sync=\"listQuery.page\" :limit.sync=\"listQuery.rows\" @pagination=\"search"+this.domain.getCapFirstPlural()+"ByFieldsByPage\" />"));
		StatementList sl = WriteableUtil.merge(sList);
		sl.setSerial(this.serial);
		return sl;
	}

	@Override
	public StatementList generateWidgetScriptStatements() {
		try {
			List<Writeable> sList = new ArrayList<>();
			

			StatementList sl = WriteableUtil.merge(sList);
			sl.setSerial(this.serial);
			return sl;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean parse() {
		return true;
	}

	public FrontDomain getDomain() {
		return domain;
	}

	public void setDomain(FrontDomain domain) {
		this.domain = domain;
	}

	public String getDetailPrefix() {
		return detailPrefix;
	}

	public void setDetailPrefix(String detailPrefix) {
		this.detailPrefix = detailPrefix;
	}

	public Set<FrontDomain> getTranlateDomains() {
		return tranlateDomains;
	}

	public void setTranlateDomains(Set<FrontDomain> tranlateDomains) {
		this.tranlateDomains = tranlateDomains;
	}

	public Set<FrontDomain> getParentTranlateDomains() {
		return parentTranlateDomains;
	}

	public void setParentTranlateDomains(Set<FrontDomain> parentTranlateDomains) {
		this.parentTranlateDomains = parentTranlateDomains;
	}

}
